#!/usr/bin/env julia
using DelimitedFiles
using Base.Filesystem: basename

metrics_dir = joinpath(pwd(), "results", "drp4dvar_vs_3dvar")
all_files = [joinpath(metrics_dir, f) for f in readdir(metrics_dir) if endswith(f, ".csv")]
files = sort(filter(x -> occursin(r"metrics_\d{8}_\d{6}\.csv", basename(x)), all_files))

out_path = "results/drp4dvar_vs_3dvar/metrics_aggregated.csv"
open(out_path, "w") do out
    println(out, "timestamp,method,cost,rms_innovation,increment_norm,solve_time,reduced_rank")
    for file in files
        m = match(r"metrics_(\d{8}_\d{6})\.csv", basename(file))
        ts = m === nothing ? "" : m.captures[1]
        data = readdlm(file, ',', header=true)[1]
        for row in eachrow(data)
            method = String(row[1])
            cost = row[2]
            rms = row[3]
            dx = row[4]
            time = row[5]
            rank = row[6]
            println(out, "$ts,$method,$cost,$rms,$dx,$time,$rank")
        end
    end
end
println("Aggregated $(length(files)) files -> $out_path")
